using UnityEngine;
using System.Collections;

public class TJSingleton<T> : TJMonoLogger where T : Component
{
	private static T instance;

	protected void Awake ()
	{
		if (instance != null && instance.gameObject != gameObject) {
			Object.Destroy (instance.gameObject);
			instance = GetComponent<T> ();
		} else if (instance == null) {
			instance = GetComponent<T> ();
		}
	}

	public static void DestroyInstance ()
	{
		if (instance != null) {
			Object.Destroy (instance.gameObject);
		}
		instance = null;
	}

	public static T Instance {
		get {
			if (instance == null) {
				instance = (T)Object.FindObjectOfType (typeof(T));
				if (instance == null) {
					GameObject obj = new GameObject (typeof(T).Name);
					obj.AddComponent<T> ();
					DontDestroyOnLoad (obj);
				}
			}
			return instance;
		}
	}

	public void Setup ()
	{
	}

	public static bool IsValidate {
		get {
			return instance != null;
		}
	}

	public void OnDestroy ()
	{
		if (instance != null && instance.gameObject == gameObject) {
			instance = null;
		}
	}
}
